package com.yzy.dao;

import com.yzy.entity.Doctors;

import com.yzy.utils.Utils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DoctorDao {
    private Utils util = new Utils();
    //zc
    public  int add(Doctors doctor){
        String sql="insert into doctor(Dname,Dage,experience,Dpass,Dsex)"+"values(?,?,?,?,?)";
        PreparedStatement ps =util.createStatement(sql);
        int result =0;
        try {
            ps.setString(1,doctor.getDname());
            ps.setString(2,doctor.getDage());
            ps.setString(3,doctor.getExperience());
            ps.setString(4,doctor.getPass());
            ps.setString(5,doctor.getDsex());

            result =ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close();
        }
        return result;
    }

    //chaxun
    public List findAll(){
        String sql = "select * from doctor";
        PreparedStatement ps = util.createStatement(sql);
        ResultSet rs = null;
        List doctorList = new ArrayList();
        try {
            rs = ps.executeQuery();

            while (rs.next()){
                Integer did=rs.getInt("did");
                String dname=rs.getString("dname");
                String dage=rs.getString("dage");
                String experience=rs.getString("experience");
                String dpass=rs.getString("dpass");
                String dsex=rs.getString("dsex");
                Doctors doctors =new Doctors(did,dname,dage,experience,dpass,dsex);
                doctorList.add(doctors);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(rs);
        }
        return doctorList;
    }

    //shanchu
    public int delete(String doctorId){
        String sql="delete from doctor where did=?";
        PreparedStatement ps =util.createStatement(sql);
        int result=0;

        try {
            ps.setInt(1,Integer.valueOf(doctorId));
            result = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close();
        }return result;
    }

    //jiuzhen
    public int updatejiuzhen(String pphone,Integer did){
        String sql="update book set state= '过期' where pphone=? and did=?";
        PreparedStatement ps=util.createStatement(sql);
        int result=0;

        try {
            ps.setString(1,pphone);
            ps.setInt(2,did);
            result = ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close();
        }return result;

    }


}
